package xyz.haijin.spider;

import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;
import xyz.haijin.spider.dao.WallpaperDetailDao;
import xyz.haijin.spider.dao.WallpaperTypeDao;
import xyz.haijin.spider.entity.WallpaperDetail;
import xyz.haijin.spider.entity.WallpaperType;
import xyz.haijin.spider.util.StringUtil;
import xyz.haijin.zt.mysql.core.BaseDAO;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author liuhaijin
 * @Date 2022/3/29
 */
public class EnterDeskListRepoPageProcessor implements PageProcessor {

    /**
     * 日志对象
     */
    private Logger logger  = LoggerFactory.getLogger(EnterDeskListRepoPageProcessor.class);


    /**
     * 壁纸类型数据库对象
     */
    private BaseDAO<WallpaperType> wallpaperTypeDao = new WallpaperTypeDao();

    /**
     * 壁纸详情数据库对象
     */
    private BaseDAO<WallpaperDetail> wallpaperDetailDao = new WallpaperDetailDao();


    private WallpaperType wallpaperType;

    public void setWallpaperType(WallpaperType wallpaperType) {
        this.wallpaperType = wallpaperType;
    }

    private static final String URL = "https://m.sj.enterdesk.com";

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000)
            //添加cookie之前一定要先设置主机地址，否则cookie信息不生效
            .setDomain("m.sj.enterdesk.com")
            //添加抓包获取的cookie信息
            .addCookie("r", "5755")
            .addCookie("t", "ea3853c33f9ea2507b37dfde365d56f5")
            .addCookie("r", "5598")
            .addCookie("t", "beaeafd0d4b258341f8b990e1388441d");

    @SneakyThrows
    @Override
    public void process(Page page) {
        Html html = page.getHtml();
        String url = page.getUrl().toString();
        System.out.println("当前url："+page.getUrl().toString());
        Selectable selectable = html.xpath("/html/body/div[15]")
                .xpath("//div[@class=\"mbig_pic_list_li\"]")
                .xpath("//dd")
                ;
        if (!url.contains(".html")) {
            if("动态".equals(wallpaperType.getTypeName())) {
                for (int i = 0; i < 4; i++) {
                    String nextUrl = wallpaperType.getTypeUrl() + "/" + (i+2) + ".html";
                    page.addTargetRequest(nextUrl);
                }
            } else {
                Selectable lastSelectable = html.xpath("/html/body/div[19]/ul/li[6]/a");
                if (lastSelectable != null) {
                    String lastUrl = lastSelectable.xpath("//a/@href").toString();
                    //        logger.info("html:"+lastSelectable.toString());
                    logger.info("lastUrl:"+lastUrl);
                    String lastUrlNum = StringUtil.getStringAfterKey(lastUrl, url);
                    String lastNum = StringUtil.getStringBetweenLastKeys("/" + lastUrlNum, "/", ".html");
                    //            System.err.println("lastUrlNum:"+lastUrlNum);
                    //            System.err.println("当前url:"+url);
                    System.err.println("lastNum:"+lastNum);
                    int num = Integer.parseInt(lastNum);
                    for (int i = 0; i < num; i++) {
                        String nextUrl = wallpaperType.getTypeUrl() + "/" + (i+2) + ".html";
                        page.addTargetRequest(nextUrl);
                    }
                }
            }
        }

        logger.info("html:"+selectable);
        for (Selectable node : selectable.nodes()) {
            String wallpaperDetailClickUrl = node.xpath("//a/@href").toString();
            String wallpaperListWidth = node.xpath("//a/img/@a_width").toString();
            String wallpaperListHeight = node.xpath("//a/img/@a_height").toString();
            String wallpaperListUrl = node.xpath("//a/img/@src").toString();
            String wallpaperName = node.xpath("//a/img/@title").toString();
            WallpaperDetail wallpaperDetail = WallpaperDetail.builder()
                    .wallpaperDetailClickUrl(wallpaperDetailClickUrl)
                    .wallpaperListWidth(Integer.valueOf(wallpaperListWidth))
                    .wallpaperListHeight(Integer.valueOf(wallpaperListHeight))
                    .wallpaperListUrl(wallpaperListUrl)
                    .wallpaperName(wallpaperName)
                    .wallpaperType(wallpaperType.getId())
                    .build();
            wallpaperDetailDao.save(wallpaperDetail);
        }



    }

    @Override
    public Site getSite() {
        return site;
    }


    public List<WallpaperType> listWallpaperType() {
        return wallpaperTypeDao.findAll();
    }

    /**
     * 获取壁纸类型请求url
     * @return
     */
    public List<String> listWallpaperTypeUrl () {
        List<WallpaperType> wallpaperTypeList = wallpaperTypeDao.findAll();
        List<String> list = new ArrayList<>();
        for (WallpaperType wallpaperType : wallpaperTypeList) {
            System.out.println(wallpaperType.getTypeName());
            if ("手机壁纸".equals(wallpaperType.getTypeName())) {
                list.add(wallpaperType.getTypeUrl());
            } else {
                list.add(URL+wallpaperType.getTypeUrl());
            }
        }
        return list;
    }

    public static void main(String[] args) {
        EnterDeskListRepoPageProcessor enterDeskDetailRepoPageProcessor = new EnterDeskListRepoPageProcessor();
        List<String> list = enterDeskDetailRepoPageProcessor.listWallpaperTypeUrl();
        for (int i = 19; i < list.size(); i++) {
            EnterDeskListRepoPageProcessor enter = new EnterDeskListRepoPageProcessor();
            List<WallpaperType> wallpaperTypes = enterDeskDetailRepoPageProcessor.listWallpaperType();
            enter.setWallpaperType(wallpaperTypes.get(i));
            String url = list.get(i);
            System.out.println("url:"+url);
            Spider.create(enter).addUrl(url).thread(5).run();
        }
    }
}
